import os

def get_file_list(image_files: list):
    teacher_data_paths = []  # 存储数据路径和分类信息
    # 根据文件夹结构获取分类信息
    for image_file in image_files:
        # 获取文件所在的子文件夹名称作为分类
        folder_name = os.path.basename(os.path.dirname(image_file))
        
        try:
            # 尝试将文件夹名称转换为整数分类标签
            category = int(folder_name)
            
            # 记录文件路径和分类，而不是立即加载数据
            teacher_data_paths.append({
                'file_path': image_file,
                'category': category,
            })
            print(f"记录文件路径：{image_file}，分类：{category}")
            
        except ValueError:
            print(f"警告：文件夹名称 {folder_name} 不是有效的分类标签，跳过文件 {image_file}")
        except Exception as e:
            print(f"处理文件 {image_file} 时出错：{e}，跳过此文件") 
    
    if len(teacher_data_paths) == 0:
        print('错误：没有找到有效的教师数据文件')
        quit()
    else:
        return teacher_data_paths


def isImgFile(path:str):
    return path.endswith('.jpg') \
        or path.endswith('.JPG') \
        or path.endswith('.jpeg') \
        or path.endswith('.JPEG') \
        or path.endswith('.png') \
        or path.endswith('.PNG')

def isSvg(path: str):
    exn = path.split('.')[-1]
    return  exn == 'sgf' or exn == 'SGF'


def load_data(path:str, args=None):
    '''加载数据'''

    if isImgFile(path):

        # 引入归一化器
        #
        # 因为这个归一化器鸿蒙系统用不了，所以我们只在这里引入它，而不是在全局引入
        from nor_imgs import ImageNormalizer

        # 创建归一化器实例
        normalizer = ImageNormalizer(target_size=args.size)

        # 加载图像（其他方法需要）
        image = normalizer.load_image(path)

        # 传统归一化方法
        if args.method == 'minmax':
            return normalizer.min_max_normalization(image, args.min, args.max)
        elif args.method == 'meanstd':
            return normalizer.mean_std_normalization(image)
        elif args.method == 'percentile':
            return normalizer.percentile_normalization(image, args.lower, args.upper)
        elif args.method == 'hist':
            return normalizer.histogram_equalization(image)
        elif args.method == 'adaptive_hist':
            return normalizer.adaptive_histogram_equalization(image, clip_limit=args.clip_limit)
    
    elif isSvg(path):

        from nor_sgf import SGFToNumpyConverter

        # 初始化转换器
        converter = SGFToNumpyConverter(board_size=19)
        
        # 处理单个SGF文件
        game = converter.load_sgf_file(path)
        if game:
            features, labels = converter.process_single_game(game, args.method)
            if len(features) > 0:
                #print(features.shape, labels.shape)
                return features, labels # 返回到外部